package com.aicare99.flutter_plugin_print;

import android.util.Log;

import com.printer.sdk.PrinterConstants;
import com.printer.sdk.PrinterConstants.Command;
import com.printer.sdk.PrinterInstance;
import com.printer.sdk.Table;


/**
 * 封装打印插件
 */
public class XTUtils {

    /**
     * 打印小票Demo
     *
     * @param mPrinter
     */
    public static void demo(PrinterInstance mPrinter) {
        mPrinter.initPrinter();
        //
        mPrinter.setFont(0, 0, 0, 0, 0);
        mPrinter.setPrinter(Command.ALIGN, Command.ALIGN_LEFT);
        mPrinter.printText("打印小票效果演示：");
        mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);
        StringBuffer sb = new StringBuffer();
        mPrinter.setPrinter(Command.ALIGN, Command.ALIGN_CENTER);
        mPrinter.setFont(0, 1, 1, 0, 0);
        mPrinter.printText("宁波月湖店\n");
        mPrinter.setPrinter(Command.ALIGN, Command.ALIGN_LEFT);
        // 字号使用默认
        mPrinter.setFont(0, 0, 0, 0, 0);
        sb.append("门店号：574001\n");
        sb.append("单据：S00003169\n");
        sb.append("收银员：s004_s004\n");
        sb.append("单据日期：2012-06-17\n");
        sb.append("打印时间：2012-06-17 13:37:24\n");
        mPrinter.printText(sb.toString()); // 打印

//        Log.e("AAAAAAAAAAAAAAAAAAAAAAA", String.valueOf(PrinterConstants.paperWidth));

        // 打印表格
        String column = "品名;数量;单价;金额";
        Table table = null;
        if (PrinterConstants.paperWidth == 384) {
            table = new Table(column, ";", new int[]{14, 6, 6, 6});
        } else if (PrinterConstants.paperWidth == 576) {
//            table = new Table(column, ";", new int[]{18, 10, 10, 12});
            table = new Table(column, ";", new int[]{14, 6, 6, 6});
        } else if (PrinterConstants.paperWidth == 724) {
            table = new Table(column, ";", new int[]{22, 14, 14, 18});
        }
        table.addRow("保鲜袋" + ";10.00;1;10.00");
        table.addRow("铁丝挂钩" + ";5.00;2;10.00");
        table.addRow("雨伞" + ";5.00;3;15.00");
        mPrinter.printTable(table);

        sb = new StringBuffer();

        if (PrinterConstants.paperWidth == 384) {
            sb.append("数量：" + "                    6.00\n");
            sb.append("总计：" + "                    35.00\n");
            sb.append("付款：" + "                    100.00\n");
            sb.append("找零：" + "                    65.00\n");
        } else if (PrinterConstants.paperWidth == 576) {
//            sb.append("数量：" + "                                6.00\n");
//            sb.append("总计：" + "                                35.00\n");
//            sb.append("付款：" + "                                100.00\n");
//            sb.append("找零：" + "                                65.00\n");
            sb.append("数量：" + "                    6.00\n");
            sb.append("总计：" + "                    35.00\n");
            sb.append("付款：" + "                    100.00\n");
            sb.append("找零：" + "                    65.00\n");
        } else if (PrinterConstants.paperWidth == 724) {
            sb.append("数量：" + "                                            6.00\n");
            sb.append("总计：" + "                                            35.00\n");
            sb.append("付款：" + "                                            100.00\n");
            sb.append("找零：" + "                                            65.00\n");
        }

        sb.append("公司名称：江苏某某家居用品有限公司" + "\n");
        sb.append("公司网址：" + "www.jiangsuxxxx.com\n");
        sb.append("地址：江苏省宁波市某某路某某号" + "\n");
        sb.append("电话：" + "0574-12345678\n");
        sb.append("服务专线：" + "4008-123-456 \n");

        if (PrinterConstants.paperWidth == 384) {
            sb.append("==============================\n");
        } else if (PrinterConstants.paperWidth == 576) {
//            sb.append("==============================================\n");
            sb.append("==============================\n");
        } else if (PrinterConstants.paperWidth == 724) {
            sb.append("================================================================\n");
        }

        mPrinter.printText(sb.toString());

        mPrinter.setPrinter(Command.ALIGN, Command.ALIGN_CENTER);
        mPrinter.setFont(0, 0, 1, 0, 0);
        mPrinter.printText("谢谢惠顾,欢迎再次光临！" + "\n");
        mPrinter.printText("Demo 版本！" + "\n\n\n");

        mPrinter.setFont(0, 0, 0, 0, 0);
        mPrinter.setPrinter(Command.ALIGN, Command.ALIGN_LEFT);

        //mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);
    }

    /**
     * 打印基础文字
     * （默认：无样式，标准字体大小，居左）
     *
     * @param mPrinter
     * @param text
     */
    public static void printText(PrinterInstance mPrinter, String text) {
        //mPrinter.initPrinter();
        //
        //mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_LEFT);
        //mPrinter.setFont(0, 0, 0, 0, 0);
        mPrinter.printText(text);
    }

    /**
     * 切纸刀动作：半切
     *
     * @param mPrinter
     */
    public static void halfCut(PrinterInstance mPrinter) {
        mPrinter.initPrinter();
        // 切纸 前空行
        mPrinter.setFont(0, 0, 0, 0, 0);
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_LEFT);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);//空2行
        // 切纸
        mPrinter.cutPaper(66, 50);//半切
    }

    /**
     * 切纸刀动作：全切
     *
     * @param mPrinter
     */
    public static void allCut(PrinterInstance mPrinter) {
        mPrinter.initPrinter();
        // 切纸 前空行
        mPrinter.setFont(0, 0, 0, 0, 0);
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_LEFT);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);//空2行
        //切纸
        mPrinter.cutPaper(65, 50);//半切
    }

    public static void setFont(PrinterInstance mPrinter, Integer mCharacterType, Integer mWidth, Integer mHeight, Integer mBold,Integer mUnderline){
        //mPrinter.initPrinter();
        //
        mPrinter.setFont(mCharacterType, mWidth, mHeight, mBold, mUnderline);
    }

    public static void alignLeft(PrinterInstance mPrinter){
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_LEFT);
    }
    public static void alignRight(PrinterInstance mPrinter){
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_RIGHT);
    }
    public static void alignCenter(PrinterInstance mPrinter){
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_CENTER);
    }
    public static void newLine(PrinterInstance mPrinter, Integer line){
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, line);//空{line}行
    }

    public static void init(PrinterInstance mPrinter){
        mPrinter.initPrinter();
    }


}
